package demo1.Factory;

import demo1.Factory.product.Product1;
import demo1.Factory.product.Product2;
import demo1.Factory.product.impl.AbstractProduct;
import demo1.Factory.product.impl.ConcreteProductA;
import demo1.Factory.product.impl.ConcreteProductC;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @ClassName AbstractFactory
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/6/2 11:06
 * @Version 1.0
 */

public class AbstractFactoryTest {
    // 多种抽象产品 对应抽象工厂中多种方法， 负责生成不同种类的产品

    public static AbstractFactory CreateFactory(Class clas) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Constructor constructor = clas.getConstructor();
        return (AbstractFactory)constructor.newInstance();
    }

    public static void main(String[] args) throws InvocationTargetException, InstantiationException, IllegalAccessException {
//        AbstractFactory abstractFactory = new Factory1();
//        Product1 product1 = abstractFactory.createProduct1();// 生产商品1
//        product1.append1();
//        Product2 product2 = abstractFactory.createProduct2();// 生产商品2
//        product2.append2();

        try {
            AbstractFactory abstractFactory = AbstractFactoryTest.CreateFactory(Factory1.class);
            Product2 product2 = abstractFactory.createProduct2();
            product2.append2();
            Product1 product1 = abstractFactory.createProduct1();
            product1.append1();
            AbstractFactory abstractFactory1 = AbstractFactoryTest.CreateFactory(Factory2.class);
            Product1 product11 = abstractFactory1.createProduct1();
            product11.append1();
            Product2 product21 = abstractFactory1.createProduct2();
            product21.append2();
        } catch (Exception e) {
            e.printStackTrace();
        }

//        Class<Factory1> factory1Class = Factory1.class;
//        try {
//            Constructor<Factory1> constructor = factory1Class.getConstructor();
//            ////如果类中没有显式定义无参构造函数，上述代码将会抛出 NoSuchMethodException 异常。
//            Factory1 factory1 = constructor.newInstance();
//            factory1.createProduct1();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        }


    }
}
// 抽象工厂类
interface AbstractFactory{
    Product1 createProduct1();
    Product2 createProduct2();
}
// 抽象产品 product1  product2
// 具体产品实现......
// 具体实现类工厂1
class Factory1 implements AbstractFactory{

    public Factory1(){

    }
    @Override
    public  Product1 createProduct1() {
        System.out.println("生产product1产品");
        return new ConcreteProductA();
    }

    @Override
    public  Product2 createProduct2() {
        System.out.println("生产product2产品");
        return new ConcreteProductC();
    }
}
// 具体实现类工厂2
class Factory2 implements AbstractFactory{

    public Factory2(){}

    @Override
    public Product1 createProduct1() {
        System.out.println("生产product1产品");
        return new ConcreteProductA();
    }

    @Override
    public  Product2 createProduct2() {
        System.out.println("生产product2产品");
        return new ConcreteProductC();
    }
}